#!/usr/bin/php
<?php
    require(__DIR__ . '/conf/config.php');
    require(__DIR__ . '/lib/spyc.php');
    
    $config = readYaml($configYaml, true);
    $build = readYaml($buildYaml);
    
    echo "oDesk sandbox helper($ver)\n===================\n";
    
    function error($msg){
        $h = fopen('php://stderr', 'w');
        fwrite($h, 'Fatal error: ');
        fwrite($h, $msg);
        fwrite($h, "\n\n");
        fclose($h);
        exit(1);
    }
    
    $mode = array();
    $help = array();
    $opt = array();
    $alias = array();
    $reqopt = array();
    $component = $config['component'];
    // $branch = 'trunk';
    
    // print_r($config);
    // exit;

    for($i = sizeof($argv);--$i>0;){
        if(preg_match('/^([\w\-:]+)(=)?/', $argv[$i], $match)){
            $data = preg_replace('/^[\w]+=/', '', $argv[$i]);
            switch($match[1]){
                case 'branch':
                    $branch = ($data != 'trunk' ? 'branches/' : '') . $data;
                    $dir = $dir == 'trunk' ? strtolower($data) : $dir;
                    break;
                case 'tag':
                    $branch = 'tags/' . $data;
                    $dir = strtolower($data);
                case 'dir':
                    $dir = $data;
                    break;
                case 'component':
                    $component = $data;
                    break;
                case 'bug':
                    $bug = preg_replace('/\\\\n/', "\n", $data);
                    break;
                case 'comment':
                    $comment = $data;
                case 'revisions':
                    $revisions = explode(',', $data);
                    break;
                case 'help':
echo "Usage: odhelper [command] [options]
where possible options is:
    branch: the name of the branch(default: trunk)
    tag:    the name of the tag
    dir:    dir/subdomain name for the branch(default: trunk)
    component: the tree name have to be processed. in case you need to specify more then one use comma as separator(no space between allowed, ie: console,www)
    bug:    number/full bug description
    comment: description of the bug(additionaly to bug number)
    revisions: revisions for merge(merge-rc command)

commands:\n";
                    exec('find ' . __DIR__ . '/lib -type f -name "*.cmd.php"', $list);
                    
                    for($i = sizeof($list); --$i >= 0;)
                        require_once($list[$i]);
                        
                    ksort($help);
                    
                    foreach($help as $k => $item) echo "\t{$k}\t\t{$item}\n";
                    exit;
                    break;
                default:
                    if(!file_exists(__DIR__ . "/lib/{$match[1]}.cmd.php")){
                        include_once(__DIR__ . '/conf/aliases.php');
                        if(!array_key_exists($match[1], $alias)) error("unknown command/option [{$match[1]}]");
                        switch(gettype($alias[$match[1]])){
                            case 'string':
                                array_push($mode, $alias[$match[1]]);
                                break;
                            case 'array':
                                $j = 0;
                                for($j = sizeof($alias[$match[1]]); --$j >= 0;)
                                    array_push($mode, $alias[$match[1]][$j]);
                                break;
                        }
                    } else array_push($mode, $match[1]);
                    break;
            }
        }
    }

    require(__DIR__ . '/lib/lib.php');
    require(__DIR__ . '/lib/svn-lib.php');
    if(!$branch || !$dir){
        switch(true){
            case is_branch_dir():
                $branch = get_branch_name();
                break;
            case $dir && is_dir($dir):
                $branch = get_branch_name($dir);
                break;
        }
    }

    while($m = array_pop($mode)){
        include_once(__DIR__ . "/lib/{$m}.cmd.php");
        $arg = array();
        foreach((array)$opt[$m] as $vn)
            array_push($arg, $$vn);
        call_user_func_array("cmd_" . preg_replace('/[\-:]/', '_', $m), $arg);
    }